package dp.onClass.class1.exer03;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int time = sc.nextInt();
        int m = sc.nextInt();
        int[][] mat = new int[m][2];
        for (int i = 0; i < m; i++) {
            mat[i][0] = sc.nextInt();
            mat[i][1] = sc.nextInt();
        }
        int[][] dp = new int[m + 1][time + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= time; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j - mat[i - 1][0] >= 0) {
                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - mat[i - 1][0]] + mat[i - 1][1]);
                }
            }
        }
        System.out.println(dp[m][time]);
    }
}
